<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" href="css/style.css" />
		<script src="https://cdn.bootcss.com/jquery/1.8.3/jquery.min.js"></script>
		<script>
		$(function(){
			page = 1; // 当前页数
			totalpage = 1;// 总页数
			getAdminInfo(1);
		});
		// 删除管理员
		function delAdmin(id){
			$.ajax({
				url : '../AdminServlet',
				method : 'post',
				data : {
					'operation' : 'del',
					'id' : id
				},
				dataType : 'json',
				success : function(info) {
					alert('删除成功!');
					getAdminInfo(page);
				}
			});
		}
		// 锁定管理员
		function lockAdmin(id, lock){
			lock = lock == '0' ? '1' : '0';
			$.ajax({
				url : '../AdminServlet',
				method : 'post',
				data : {
					'operation' : 'lock',
					'id' : id,
					'lock' : lock
				},
				dataType : 'json',
				success : function(info) {
					getAdminInfo(page);
				}
			});
		}
		// 获取管理员信息
		function getAdminInfo(page, name){
			page = page > totalpage ? totalpage : page;
			page = page < 1 ? 1 : page;
			$.ajax({
				url : '../AdminServlet',
				method : 'post',
				data : {
					'operation' : 'getall',
					'page' : page,
					'name' : name
				},
				dataType : 'json',
				success : function(json) {
					str = " <tr class=\"tb_tr_center\">" + 
			    	"			<th>#</th>" + 
			    	"			<th>用户名</th>" + 
			    	"			<th>密码</th>" + 
			    	"			<th>状态</th>" + 
			    	"			<th>操作</th>" + 
			    	"		</tr>";
					$.each(json, function(index, info) {
						if (info.total == null && info.id != null){
							locked = info.locked == '0' ? '正常' : '已锁定';
							lockit = info.locked == '0' ? '锁定' : '解锁';
							str += "<tr class=\"tr_vertical_align\">" + 
					    	"			<td class=\"tb_tr_center\">"+info.id+"</td>" + 
					    	"			<td class=\"tb_tr_center\">"+info.username+"</td>" + 
					    	"			<td class=\"tb_tr_center\">"+info.password+"</td>" + 
					    	"			<td class=\"tb_tr_center\"><input type=\"button\" value=\""+locked+"\" class=\"greenbutton\"/></td>" + 
					    	"			<td class=\"operation tb_tr_center\"><a href=\"altadmin.html?id="+info.id+"\">&nbsp;修改&nbsp;</a><a href='javascript:void(0)' onclick=\"delAdmin("+info.id+")\">&nbsp;删除&nbsp;</a><a href='javascript:void(0)' onclick=\"lockAdmin("+info.id+","+info.locked+")\">&nbsp;"+lockit+"&nbsp;</a></td>" + 
					    	"		</tr>";
						}
						else{
							totalpage = info.total / 10;
							totalpage = totalpage > parseInt(totalpage) ? parseInt(totalpage) + 1 : totalpage;
							$('#total').empty();
							$('#total').append(info.total);
							$('#curpage').empty();
							$('#curpage').append(page);
							$('#totalpage').empty();
							$('#totalpage').append(totalpage);
						}
					});
					$('#adminTable').empty();
					$('#adminTable').append(str);
				}
			});
		}
		</script>
	</head>
	<body>
		<div class="main border-bottom-gray"><span class="fontBold"><a href="" class="mapsite">首页</a></span>&nbsp;/&nbsp;
			<span class="fontBold"><a href="" class="mapsite">系统管理</a></span>&nbsp;/&nbsp;管理员管理</div>
		
		<div class="border-bottom-gray main">
			<input id="name" type="text" class="input_text" placeholder="请输入管理员用户名"/>&nbsp;
			<input type="button" value="查询" class="input_submit" onclick="getAdminInfo(1,$('#name').val())"/>&nbsp;&nbsp;
			<input type="button" value="添加管理员" class="input_submit" onclick="location='addadmin.html'"/>&nbsp;&nbsp;
		</div>
		<div class="main graybg">
			<table class="tb_wid" id="adminTable">
			</table>
			<!-- <div align="left" valign="top" class="fenye">
				<span id="total"></span>条数据 <span id="curpage"></span>/<span id="totalpage"></span> 页&nbsp;&nbsp;
				<a href="javascript:void(0)" target="mainFrame" onfocus="this.blur()" onclick="getAdminInfo(1)">首页</a>&nbsp;&nbsp;
				<a href="javascript:void(0)" target="mainFrame" onfocus="this.blur()" onclick="getAdminInfo(--page)">上一页</a>&nbsp;&nbsp;
				<a href="javascript:void(0)" target="mainFrame" onfocus="this.blur()" onclick="getAdminInfo(++page)">下一页</a>&nbsp;&nbsp;
				<a href="javascript:void(0)" target="mainFrame" onfocus="this.blur()" onclick="getAdminInfo(totalpage)">尾页</a>
        	</div> -->
		</div>
	</body>
</html>
